# -*- encoding:utf8 -*-

import sys
import json
import time
from django.conf import settings
import requests
appid = settings.APPID
secret = settings.SECRET
sys.weixin={}

def get_access_token():
    """获取token，如果没过期(1个小时)，就从缓存里面取，否则从服务器取。
    注意：
        尽管可能在分布式服务器上，全局变量不同，但是这里不要求sys.weixin同步，所以这里应该是没有问题的。
    """
    access_token= sys.weixin.get('access_token')
    if not access_token:
        access_token = _token_from_sever()
    else:
        token_time =sys.weixin['token_time']
        now = int(time.time())
        if now -token_time >3600:
            access_token=_token_from_sever()
    return access_token

def get_code_from_server():
    url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri=\
        http%3a%2f%2fwechat.mokitech.com/attandanc/recieve_code%2f&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect' 

def embed_url(url):
    return 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+url\
        +'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'
    

def userid_from_code(code):
    token=get_access_token()
    if not token:
        return 'token is none'
    url='https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='+token+'&code='+code
    rt=requests.get(url)
    dc=json.loads(rt.text)
    return dc.get('UserId')

# def recieve_code(request):
    # """测试用的，测试微信auth2.0,将连接嵌入第三方，是否能够获取用户信息。
    # """
    # code = request.GET.get('code')
    # if code:
        # openid = get_openid(code)
        # if not openid:
            # return HttpResponse('not openid')
        # else:
            # items = get_user_with_openids([openid,])
            # if not items:
                # return HttpResponse('not items')
            # else:
                # item= items[0]
                # return HttpResponse("<div>nickname:%s,,,<img src='%s'></div>"%(item.get('nickname'),item.get('headimgurl')))   
    # else:
        # return HttpResponse('not code')
    

def _token_from_sever():
    
    """从服务器取token
    """
   
    rq = requests.get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='+appid+'&corpsecret='+secret)
    dc=json.loads(rq.text)
    access_token = dc.get("access_token")
    token_time=int(time.time())
    sys.weixin['access_token']=access_token
    sys.weixin['token_time']=token_time  
    return access_token


def send_text(userid,content):
    """
    微信中发送【文字】消息
    
    @type userid:str ,员工号，如AE1856
    @type content:str ,发送的内容
    """
    token=get_access_token()
    url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='+token
    data={
        "touser": "%s"%userid,
        # "toparty": " PartyID1 | PartyID2 ",
        # "totag": " TagID1 | TagID2 ",
        "msgtype": "text",
        "agentid": "3",
        "text": {
            "content": content.decode('utf8')
            },
        "safe":"0",
    }  
    data=json.dumps(data,ensure_ascii=False)
    rt = requests.post(url,data=data.encode('utf8'))
    return json.loads(rt.text)
    
def weixin_view(request):
    """现在没用
    """
    if request.method == 'GET':
        s1 = request.GET.get('echostr','')
        if s1:
            return  HttpResponse(s1)
        else:    # 管理抢答界面
            return HttpResponse('weixin get ok')
    elif request.method == 'POST':
        if request.META.get('CONTENT_TYPE','').lower().strip() == 'text/xml':
            come_str = request.body 
            handle = paser(come_str)
            if handle.msgType=='text':
                #can_qd= state.can_qd #mystate.get_qd_state()
                if  gd.can_qd :
                    openid= handle.fromUserName
                    msg = get_or_none(QiangDaModel,openid=openid)
                    # msg =None
                    if msg:
                        content = '每个问题只能抢答一次'
                    else:
                        try:
                            content ='回答成功!' 
                            now=timezone.localtime(timezone.now()).time().strftime('%H:%M:%S')
                            user= get_or_none(UserModel,openid=openid)
                            if user:
                                nickname,headimg = user.nickname(),user.img()
                            else:
                                nickname,headimg = '',''
                            
                           # //--memecatched
                            # index = gd.incr('q_cnt')
                            # def func(ls):
                                # ls.append({'id':index, #gd.q_cnt,
                                        # 'openid':openid,
                                        # 'content':handle.content,
                                        # 'time':now,
                                        # 'nickname':nickname,
                                        # 'head':headimg})
                                # return ls
                            # gd.sync('qiangda_msgs',func)
                            
                            # cont = base64.b64encode(handle.content)
                            # nick = base64.b64encode(nickname.encode('utf8'))
                            # QiangDaModel(openid=openid,content=cont,time=now,submited='false',nickname=nick,head=headimg).save()
                            
                            cont = base64.b64encode(handle.content)
                            # cont=handle.content
                            nick = nickname.encode('utf8')
                            QiangDaModel(openid=openid,content=cont,time=now,submited='false',nickname=nick,head=headimg).save()                        
                            # gd.has_qd_msg=True     # 标记有数据，减少读取数据库次数
                        except Exception as e:
                            ExceptionModel(place='weixin',detail=str(e)).save()
                else:
                    content ='抢答还没有开始'
                    
                text_msg= """
                <xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[text]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                </xml>
                """%(handle.fromUserName, handle.toUserName,int(time.time()),content)  
                return HttpResponse(text_msg, content_type='text/html; charset=utf-8')
        return HttpResponse('success')